package project2.ui.game.auto;

import project2.config.Config;
import project2.config.ImageConstants;
import project2.resources.Images;
import project2.ui.game.GamePlayingPanel;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ScoreShow {
    GamePlayingPanel panel;

    public ScoreShow(GamePlayingPanel panel) {
        this.panel = panel;
    }

    public void run(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        // 按位拆解积分数字，存储到集合中
        List<Integer> intList = new ArrayList<>();
        int score = panel.score.get();
        int quotient;
        while ((quotient = score / 10) != 0) {
            intList.add(score % 10);
            score = quotient;
        }
        intList.add(score % 10);

        // 绘制位置
        int posX = Config.SCORE_IMG_POS_X;
        int posY = Config.SCORE_IMG_POS_Y;

        // 绘制符号“||”
        g2d.drawImage(Images.SCORE_IMG, posX, posY, ImageConstants.SCORE_IMG_WIDTH, ImageConstants.SCORE_IMG_HEIGHT, panel);
        posX += ImageConstants.SCORE_IMG_WIDTH;
        posY += ImageConstants.SCORE_IMG_HEIGHT - ImageConstants.NUMBER_0_HEIGHT;

        // 按位绘制数字
        int size = intList.size();
        for (int i = size - 1; i >= 0; i--) {
            switch (intList.get(i)) {
                case Config.NUMBER_0:
                    g2d.drawImage(Images.NUMBER_0_IMG, posX, posY, ImageConstants.NUMBER_0_WIDTH, ImageConstants.NUMBER_0_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_0_WIDTH;
                    break;
                case Config.NUMBER_1:
                    g2d.drawImage(Images.NUMBER_1_IMG, posX, posY, ImageConstants.NUMBER_1_WIDTH, ImageConstants.NUMBER_1_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_1_WIDTH;
                    break;
                case Config.NUMBER_2:
                    g2d.drawImage(Images.NUMBER_2_IMG, posX, posY, ImageConstants.NUMBER_2_WIDTH, ImageConstants.NUMBER_2_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_2_WIDTH;
                    break;
                case Config.NUMBER_3:
                    g2d.drawImage(Images.NUMBER_3_IMG, posX, posY, ImageConstants.NUMBER_3_WIDTH, ImageConstants.NUMBER_3_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_3_WIDTH;
                    break;
                case Config.NUMBER_4:
                    g2d.drawImage(Images.NUMBER_4_IMG, posX, posY, ImageConstants.NUMBER_4_WIDTH, ImageConstants.NUMBER_4_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_4_WIDTH;
                    break;
                case Config.NUMBER_5:
                    g2d.drawImage(Images.NUMBER_5_IMG, posX, posY, ImageConstants.NUMBER_5_WIDTH, ImageConstants.NUMBER_5_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_5_WIDTH;
                    break;
                case Config.NUMBER_6:
                    g2d.drawImage(Images.NUMBER_6_IMG, posX, posY, ImageConstants.NUMBER_6_WIDTH, ImageConstants.NUMBER_6_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_6_WIDTH;
                    break;
                case Config.NUMBER_7:
                    g2d.drawImage(Images.NUMBER_7_IMG, posX, posY, ImageConstants.NUMBER_7_WIDTH, ImageConstants.NUMBER_7_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_7_WIDTH;
                    break;
                case Config.NUMBER_8:
                    g2d.drawImage(Images.NUMBER_8_IMG, posX, posY, ImageConstants.NUMBER_8_WIDTH, ImageConstants.NUMBER_8_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_8_WIDTH;
                    break;
                case Config.NUMBER_9:
                    g2d.drawImage(Images.NUMBER_9_IMG, posX, posY, ImageConstants.NUMBER_9_WIDTH, ImageConstants.NUMBER_9_HEIGHT, panel);
                    posX += ImageConstants.NUMBER_9_WIDTH;
                    break;
            }
        }
    }
}
